

package com.suzj.extraenchantments.event;


import com.suzj.extraenchantments.ExtraEnchantments;
import com.suzj.extraenchantments.enchant.EnchantRegistry;
import com.suzj.extraenchantments.util.EffectUtil;
import com.suzj.extraenchantments.util.EnchantmentUtil;
import com.suzj.extraenchantments.util.GetEntityHelper;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.monster.*;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.entity.projectile.FireworkRocketEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
import net.minecraftforge.event.entity.living.*;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;

import java.util.ArrayList;
import java.util.Random;

@EventBusSubscriber(modid = ExtraEnchantments.MODID)
public class ServerEvent {
    public static final Random random = new Random();
    private static final ResourceLocation QUESTION_MARK_TEXTURE = ResourceLocation.fromNamespaceAndPath(ExtraEnchantments.MODID, "textures/particle/wenhao_particle.png");

    @SubscribeEvent
    public static void potionTransit(LivingAttackEvent fEvent) {
        if (EnchantmentUtil.isNormalDamageType(fEvent.getSource())) {
            if (fEvent.getSource().getEntity() instanceof LivingEntity attacker) {
                LivingEntity victim = fEvent.getEntity();
                if (victim != null) {
                    int level = EnchantmentHelper.getEnchantmentLevel((Enchantment)EnchantRegistry.flowmytears.get(), attacker);
                    if (level > 0) {
                        for(var effectInstance :  attacker.getActiveEffects().stream().filter((effectInstancex) -> effectInstancex.getEffect().getCategory() == MobEffectCategory.HARMFUL).filter((effectInstancex) -> effectInstancex.isCurativeItem(new ItemStack(Items.MILK_BUCKET))).filter(EffectUtil::isEffectShown).toList()) {
                            victim.addEffect(effectInstance);
                        }

                    }
                }
            }
        }
    }

    @SubscribeEvent
    public static void onRepair(LivingEvent.LivingTickEvent event) {
        LivingEntity entity = event.getEntity();
        if (entity != null) {
            int level = EnchantmentHelper.getEnchantmentLevel(EnchantRegistry.MicroscopicReordering.get(), entity);
            if (level > 0) {
                if (entity instanceof Player player) {
                    ArrayList<ItemStack> enchantedItems = EnchantmentUtil.findEnchantedItems(player, (Enchantment)EnchantRegistry.MicroscopicReordering.get());
                    if (enchantedItems.isEmpty()) {
                        return;
                    }

                    long time = player.level().getGameTime();
                    if (time % 120L == 0L) {
                        for(ItemStack itemStack : enchantedItems) {
                            itemStack.setDamageValue(itemStack.getDamageValue() - 1);
                        }
                    }
                }

            }
        }
    }

    @SubscribeEvent
    public static void onHeadDrop(LivingDropsEvent fEvent) {
        if (fEvent.getSource().getEntity() instanceof LivingEntity) {
            LivingEntity attacker = (LivingEntity)fEvent.getSource().getEntity();
            if (attacker != null) {
                LivingEntity victim = fEvent.getEntity();
                if (victim != null) {
                    int level = EnchantmentHelper.getEnchantmentLevel((Enchantment)EnchantRegistry.Behead.get(), attacker);
                    if (level > 0) {
                        ItemStack itemHead = null;
                        if (fEvent.getEntity() instanceof Creeper) {
                            itemHead = new ItemStack(Items.CREEPER_HEAD);
                        }

                        if (fEvent.getEntity() instanceof Skeleton) {
                            itemHead = new ItemStack(Items.SKELETON_SKULL);
                        }

                        if (fEvent.getEntity() instanceof Zombie) {
                            itemHead = new ItemStack(Items.ZOMBIE_HEAD);
                        }

                        if (fEvent.getEntity() instanceof Player) {
                            itemHead = new ItemStack(Items.PLAYER_HEAD);
                        }

                        if (fEvent.getEntity() instanceof WitherSkeleton) {
                            itemHead = new ItemStack(Items.WITHER_SKELETON_SKULL);
                        }

                        if (itemHead != null) {
                            ItemEntity entityItem = new ItemEntity(fEvent.getEntity().level(), fEvent.getEntity().getX(), fEvent.getEntity().getY(), fEvent.getEntity().getZ(), itemHead);
                            fEvent.getDrops().add(entityItem);
                        }

                    }
                }
            }
        }
    }

    @SubscribeEvent
    public static void onEntityJoinWorld(EntityJoinLevelEvent event) {
        if (event.getEntity() instanceof ItemEntity) {
            ItemEntity itemEntity = (ItemEntity)event.getEntity();
            ItemStack itemStack = itemEntity.getItem();
            if (EnchantmentHelper.getItemEnchantmentLevel((Enchantment)EnchantRegistry.Quenching.get(), itemStack) > 0) {
                itemEntity.setSecondsOnFire(-1);
                itemEntity.setInvulnerable(true);
            }
        }

    }

    @SubscribeEvent
    public static void giveBuff(LivingEvent.LivingTickEvent event) {
        LivingEntity entity = event.getEntity();
        if (entity != null) {
            int level = EnchantmentHelper.getEnchantmentLevel((Enchantment)EnchantRegistry.TemperedWings.get(), entity);
            if (level > 0) {
                int totalArmorValue = entity.getArmorValue();
                if (totalArmorValue < 20 && !entity.hasEffect(MobEffects.DAMAGE_RESISTANCE)) {
                    entity.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE));
                }

            }
        }
    }

    @SubscribeEvent
    public static void onLivingEntityUseItemFinish(ProjectileImpactEvent event) {
        Entity entity = event.getEntity();
        if (entity instanceof Arrow arrow) {

          var arrowMotion = arrow.getDeltaMovement();
            Player shooter = GetEntityHelper.getNearestPlayer(arrow);
            if (shooter != null) {
                int l = EnchantmentHelper.getEnchantmentLevel((Enchantment)EnchantRegistry.Firecracker.get(), shooter);
                if (l > 0) {
                    var world = entity.getCommandSenderWorld();
               var position = entity.position();
                    ItemStack fireworkItem = GetEntityHelper.createFireworkItem();
                    FireworkRocketEntity fireworkRocket = new FireworkRocketEntity(world, position.x, position.y, position.z, fireworkItem);
                    fireworkRocket.setDeltaMovement(arrowMotion.x, arrowMotion.y, arrowMotion.z);
                    world.addFreshEntity(fireworkRocket);
                }
            }
        }
    }

    @SubscribeEvent
    public static void onMonsterDeath(LivingDeathEvent fEvent) {
        LivingEntity entityLiving = fEvent.getEntity();
        if (entityLiving != null) {
            if (fEvent.getSource().getEntity() instanceof LivingEntity) {
                LivingEntity attacker = (LivingEntity)fEvent.getSource().getEntity();
                if (attacker != null) {
                    int l = EnchantmentHelper.getEnchantmentLevel((Enchantment)EnchantRegistry.TheCallOfPlague.get(), attacker);
                    if (l > 0) {
                       var list = new ArrayList<MobEffectInstance>();

                        for(var effectInstance : entityLiving.getActiveEffects()) {
                            if (effectInstance.getEffect().getCategory() == MobEffectCategory.HARMFUL) {
                                list.add(effectInstance);
                            }
                        }

                        for(Monster monster : attacker.level().getEntitiesOfClass(Monster.class, GetEntityHelper.getBoundingBoxAroundEntity(attacker, (double)10.0F))) {
                            if (monster.isAlive()) {
                                for(var effectInstance : list) {
                                    monster.addEffect(effectInstance);
                                }
                            }
                        }

                    }
                }
            }
        }
    }
}
